<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'订单总列表'),
);
$return['status']=$this->fuwuorder_status;
$return['order_types']=$this->order_types;
if($_RQ['op']=='list'){
	pdo_update('jy_shopbaoxiu_order',array('ispay'=>1),array('uniacid'=>$_SESSION['uniacid'],'ispay'=>0,'pay_id >'=>0,'status'=>array(1,2,4,5,6,7)));
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_shopbaoxiu_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$get_pay_types=get_pay_types();
		foreach($list as &$item){
			$item['serve_title']=pdo_getcolumn('jy_shopbaoxiu_list',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pay_id']),'pay_type');
			if($item['isyuejie'] && !$item['paytype']){
				$item['paytype']='yuejie';
			}
			$item['paytypetitle']=$get_pay_types[$item['paytype']];
			$item['worker_name']=pdo_getcolumn('jy_wxshifu_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['shifuid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('type'=>'string_more','title'=>'客户信息','data'=>array(
				array('title'=>'姓名：','name'=>'username'),
				array('title'=>'电话：','name'=>'mobile'),
				array('title'=>'地址：','name'=>'address')
				)
			),
			array('name'=>'worker_name','title'=>'师傅/技术员','type'=>'string'),
			array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'order_types'),
			array('name'=>'serve_time','title'=>'服务时间','type'=>'time'),
			array('type'=>'string_more','title'=>'金额','data'=>array(
				array('title'=>'金额：','name'=>'price'),
				array('title'=>'支付：','name'=>'payfee')
				)
			),
			array('name'=>'paytypetitle','title'=>'支付方式','type'=>'string'),
			array('name'=>'ispay','title'=>'支付状态','type'=>'radio','options'=>'ispay'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
		),'action'=>array(
			//array('type'=>'href','op'=>'look','title'=>'自定义表单数据'),
			array('type'=>'post','op'=>'confirm','title'=>'确认','show'=>array('status'=>1,'type'=>1)),
			array('type'=>'modal','op'=>'sendto','title'=>'派单','show'=>array('status'=>array(1,8),'type'=>0)),
			array('type'=>'post','op'=>'cancel','title'=>'取消','message'=>'取消订单？','show'=>array('status'=>1)),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'用户姓名','type'=>'string'),
			array('name'=>'worker_name','width'=>15,'title'=>'员工姓名','type'=>'string'),
		)
	);
	$return['ispay']=array('未支付','已支付');
	$return['htmls']=$htmls;
	exi($return);
}
//确认订单操作
if($_RQ['op']=='confirm'){
	if($_RQ['id']){
		pdo_update('jy_shopbaoxiu_order',array('status'=>'2'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('处理成功');
}
if($_RQ['op']=='sendto'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['type']){
			exi('到店维修订单无需指派！','error');
		}
		if(!$post['shifu_id'] && $post['need_setpeisong']){
			exi('请选择技术员/师傅！','error');
		}
		if($post['shifu_id']){
			$post['order_id']=pdo_getcolumn('jy_wxshifu_order',array('orderid'=>$post['id'],'uniacid'=>$_SESSION['uniacid'],'plugin'=>'jy_shopbaoxiu'),'id');
			if(!$post['order_id']){
				$post['sendtype']=1;
				$post['order_id']=model('jy_wxshifu/model')->addorder($post,0,'jy_shopbaoxiu');
			}
			model('jy_wxshifu/order')->order_sendget($post);
		}else{
			model('jy_wxshifu/model')->addorder($post,0,'jy_shopbaoxiu');
			pdo_update('jy_shopbaoxiu_order',array('status'=>8),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		}			
		exi('订单处理成功！','','/fuwu.order/list/');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请选择订单！','error');
	}
	if(!$this->plugin_exists('jy_wxshifu')){
		exi('请联系管理员开通《维修师傅》插件！','error');
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单处理',
	);
	$item=pdo_get('jy_shopbaoxiu_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['serve_title']=pdo_getcolumn('jy_shopbaoxiu_list',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$tip='选择师傅后，将直接指派；不选择师傅，就表示让师傅自己抢单；';
	$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'copy','title'=>'联系人','name'=>'username'),
			array('type'=>'copy','title'=>'联系电话','name'=>'mobile'),
			array('type'=>'copy','title'=>'上门地址','name'=>'address'),
			array('type'=>'copy','title'=>'项目','name'=>'serve_title'),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定技术员/师傅？','help'=>'不指定，则由技术员/师傅自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定技术员/师傅','name'=>'shifu_id','options'=>'peisongs','help'=>'只能指定该门店下面的技术员/师傅，或本城市的平台技术员/师傅！','show'=>array('need_setpeisong'=>1)),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	//多商户用户，选择平台区域配送员
	if($item['storeid']){
		$areas=pdo_getall('jy_wxshifu_area',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>array($item['storeid'],0)));
		foreach($areas as $v){
			$users=pdo_getall('jy_wxshifu_member',array('uniacid'=>$_SESSION['uniacid'],'group_id'=>$v['id'],'status'=>1),array('id','title'));
			foreach($users as $uv){
				$peisongs[]=$uv;
			}
		}
	}else{
		$peisongs=pdo_getall('jy_wxshifu_member',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'));
	}
	$return['peisongs']=$peisongs;
	exi($return);
}
//取消订单操作
if($_RQ['op']=='cancel'){
	if($_RQ['id']){
		$this->model('model')->order_cancel($_RQ['id']);
	}
	exi('取消订单成功！');
}
//同意退款
if($_RQ['op']=='agree'){
	if($_RQ['id']){
		pdo_update('jy_shopbaoxiu_order',array('status'=>'6'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('已同意退款申请！');
}
//拒绝退款操作
if($_RQ['op']=='disagree'){
	if($_RQ['id']){
		pdo_update('jy_shopbaoxiu_order',array('status'=>'7'),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	exi('已拒绝退款申请！');
}

if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_shopbaoxiu_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择订单','error');
	}
	$user=pdo_get('jy_shopbaoxiu_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['worker_name']=pdo_getcolumn('jy_shopbaoxiu_worker',array('id'=>$item['worker_id'],'uniacid'=>$_SESSION['uniacid']),'name');
			$item['worker_name']=$item['worker_name']?$item['worker_name']:'暂无';
			$item['serve_title']=pdo_getcolumn('jy_shopbaoxiu_serve',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
			//$item['username']=$user['name'];
			//$item['user_mobile']=$user['mobile'];
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id'],'plugin'=>'jy_shopbaoxiu'),'pay_type');
			$item['paytype']=get_pay_types()[$item['paytype']];
			$package=json_decode($item['package'],true);
			$item['package']=$package['package_title'];
			$serve_times=pdo_getcolumn('jy_shopbaoxiu_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$item['day'].'--'.$serve_times[$item['timeid']]['times']['start'].'-'.$serve_times[$item['timeid']]['times']['end'];
			$item['total']=$item['total']?:1;
	$item['fileds']=$item['fileds']?json_decode($item['fileds'],true):array();
	$formtypes=get_form_types();	
	foreach($item['fileds'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回订单列表'),
		),
		'desc'=>'订单信息',
		'look'=>array(
				array('name'=>'username','title'=>'用户姓名','type'=>'string'),
				array('name'=>'worker_name','title'=>'员工姓名','type'=>'string'),
				array('name'=>'mobile','title'=>'用户电话','type'=>'string'),
				array('name'=>'serve_title','title'=>'项目名称','type'=>'string'),
				array('name'=>'package','title'=>'套餐','type'=>'string'),
				array('name'=>'serve_time','title'=>'服务时间','type'=>'string'),
				array('name'=>'money','title'=>'金额','type'=>'string'),
				array('name'=>'pay','title'=>'实际支付','type'=>'string'),
				array('name'=>'remark','title'=>'备注','type'=>'string'),
				array('name'=>'coupon','title'=>'优惠券','type'=>'string'),
				array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
				array(
						'title'=>'表单详情',
						'type'=>'array',
						'name'=>'fileds',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
				)
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_shopbaoxiu_order',$params);
	if($order){
		foreach($order as &$item){
			$item['serve_title']=pdo_getcolumn('jy_shopbaoxiu_list',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['paytype']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'oid'=>$item['id'],'plugin'=>'jy_shopbaoxiu'),'pay_type');
			$item['paytype']=get_pay_types()[$item['paytype']];
			$item['serve_time']=date('Y-m-d H:i:s',$item['serve_time']);
			$item['typetitle']=$this->order_types[$item['type']];
			$item['status']=$this->fuwuorder_status[$item['status']];
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'username'=>'用户姓名',
		'mobile'=>'用户电话',
		'serve_title'=>'项目名称',
		'status'=>'类型',
		'serve_time'=>'服务时间',
		'price'=>'金额',
		'payfee'=>'实际支付',
		'paytype'=>'支付方式',
		'remark'=>'备注',
		'status'=>'状态',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
